var importTitle;
var importType;
var importVM;
var importedDetails;

Object.size = function(obj) {
	var size = 0, key;
	for (key in obj) {
		if (obj.hasOwnProperty(key))
			size++;
	}
	return size;
};

var importViewModel = function() {

	var self = this;

	self.boardDetails = ko.observableArray();
	self.selectedBoard = ko.observable();
	self.importType = ko.observable();
	self.importedDetails = ko.observableArray();

	self.importItems = function() {
		console.log("Importing items------->", self.selectedBoard().board);
		showLoader(true, TYPE_LOADING);
		jQuery.ajax({
			url : "/ce-static-testedge/common/data/" + self.selectedBoard().filename,
			type : "get",
			success : function(result) {
				console.log("result:", result);
				importedDetails = result;
				importingData();
				
				showLoader(false);
				$("#dialog-import").dialog("close");	
			},
			error : function(jqXHR, textStatus, errorThrown) {
				showLoader(false);
				console.log("error", jqXHR);
				console.log("error", textStatus);
			}
		});
	}

	self.cancelItem = function() {
		console.log("Cancel item------->");
		$("#dialog-import").dialog("close");
	}
}

function loadConfig() {
	console.log("loading config details");

	jQuery.ajax({
		url : "/ce-static-testedge/common/data/config.json",
		type : "get",
		success : function(result) {
			console.log("result:", result);
			var boardArr = new Array();
			for (var i = 0; i < result.config.imports[importType].length; i++) {
				if(result.config.imports[importType][i].active)
				{
					boardArr.push(result.config.imports[importType][i]);
				}
			}
			console.log("boardArr.length:", boardArr.length);

			importVM.boardDetails(boardArr);
		},
		error : function(jqXHR, textStatus, errorThrown) {
			console.log("error", jqXHR);
			console.log("error", textStatus);
		}
	});

}


$(document).ready(function() {
	console.log("document Ready");
	importVM = new importViewModel();

	ko.applyBindings(importVM, document.getElementById("importContainer"));
	
//	jQuery("#dialog-import").title("Import "+importTitle);
	loadConfig();

});
